package edu.hawaii.myisern.model;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * A helper class to convert between String and list.
 * 
 * @author Edward Kim
 * @author JianFei Liao
 * @author KaYee Leung
 */
public class ListStringConverter {
  /**
   * Converts a list of String to a String with commas.
   * 
   * @param list The list to convert.
   * @return A String.
   */
  public String listToString(List<String> list) {
    String string = "";
    // use comma to dilemma every element
    for (String curString : list) {
      string = string + curString + ", ";
    }
    string = string.substring(0, string.lastIndexOf(", "));
    return string;
  }
  
  /**
   * Converts a String with commas to a list of String.
   * 
   * @param string The String to convert.
   * @return A list of String.
   */
  public List<String> stringToList(String string) {
    List<String> list = new ArrayList<String>();
    if (string == null) {
      return list;
    }
    String tempString = string;
    // parse the string by dilemma comma
    int index = tempString.indexOf(',');
    String element = "";
    while (index != -1) {
      element = tempString.substring(0, index);
      list.add(element);
      tempString = tempString.substring(index + 1).trim();
      index = tempString.indexOf(',');
    }
    list.add(tempString.trim());
    return list;
  }
  
  /**
   * Converts a list of BigInteger to a String with commas.
   * 
   * @param list The list to convert.
   * @return A String.
   */
  public String bigIntegerListToString(List<BigInteger> list) {
    String string = "";
    // use comma to dilemma every element
    for (BigInteger curInteger : list) {
      String curString = "" + curInteger.intValue();
      string = string + curString + ", ";
    }
    string = string.substring(0, string.lastIndexOf(", "));
    return string;
  }
  
  /**
   * Converts a String with commas to a list of BigInteger.
   * 
   * @param string The String to convert.
   * @return A list of BigInteger.
   */
  public List<BigInteger> stringToBigIntegerList(String string) {
    List<BigInteger> list = new ArrayList<BigInteger>();
    if (string == null) {
      return list;
    }
    String tempString = string;
    BigInteger number; 
    // parse the string by dilemma comma
    int index = tempString.indexOf(',');
    String element = "";
    while (index != -1) {
      element = tempString.substring(0, index);
      number = new BigInteger(element);
      list.add(number);
      tempString = tempString.substring(index + 1).trim();
      index = tempString.indexOf(',');
    }
    number = new BigInteger(tempString.trim());
    list.add(number);
    return list;
  }
}